<?php


namespace app\home\controller;

use app\home\model\Index as IndexModel;

use app\home\model\User;
use think\Controller;
use app\home\model\User as UserModel;
use think\Db;
use think\Session;


class Index extends Controller
{

    public function index()
    {
        $indexModel = new IndexModel();
        $result = $indexModel->index();
        if ($result) {
            return view('home@index/index', [
                'category' => $result
            ]);
        } else {
            alert('查询失败');
        }
    }

    public function loadIndex()
    {
        $indexModel = new IndexModel();
        $result = $indexModel->index();
        if (Session::get('name')) {
            return view('home@index/index2',['category' => $result]);
        }else{
            return $this->error('请登录', '/');
        }

    }

    public function reguser()
    {

        parse_str(input('post.str'), $arr);

        $arr['regtime'] = time();
        $arr['status'] = 1;

        $user = new UserModel();
        $row = $user->adduser($arr);

        if ($row) {
            echo '注册成功!';
        } else {
            echo '注册失败,请重试!';
        }

    }

    public function logindo()
    {
        parse_str(input('post.mes'), $arr);
        $user = new UserModel();
        $row = $user->checkLogin($arr);
        //用户存在情况下
        if ($row) {
            Session::set('name', [
                'name' => $row['nickname'],
                'id' => $row['id'],
            ]);

            return $this->success('登录成功', 'home/index/loadIndex');
        } else {
            echo '账号或密码有误!';
        }
    }

    public function loginout()
    {
        Session::clear();
        return $this->success('退出成功~', '/');
    }
}
